package whr.modules.assets.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import whr.common.utils.PageUtils;
import whr.common.utils.Query;

import whr.modules.assets.dao.ReceiveDao;
import whr.modules.assets.dao.ScrapDao;
import whr.modules.assets.entity.InfoEntity;
import whr.modules.assets.entity.ReceiveEntity;
import whr.modules.assets.entity.RepairsEntity;
import whr.modules.assets.entity.ScrapEntity;
import whr.modules.assets.service.InfoService;
import whr.modules.assets.service.ReceiveService;
import whr.modules.assets.service.RepairsService;
import whr.modules.assets.service.ScrapService;


@Service("scrapService")
public class ScrapServiceImpl extends ServiceImpl<ScrapDao, ScrapEntity> implements ScrapService {
    @Autowired
    private ScrapService scrapService;
    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private RepairsService repairsService;
    @Autowired
    private InfoService infoService;
    @Autowired
    private ReceiveDao receiveDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ScrapEntity> page = this.page(
                new Query<ScrapEntity>().getPage(params),
                new QueryWrapper<ScrapEntity>()
                        .like(StringUtils.isNotBlank((String) params.get("key")),"assets_num",params.get("key"))
        );

        return new PageUtils(page);
    }
    @Override
    public PageUtils queryByRole(Map<String, Object> params) {
        //根据id查角色
        Long userId = Long.parseLong((String) params.get("userId"));
        List<String> userRoles = receiveDao.selectUserRoles(userId);
        for (String userRole :userRoles  ) {  // 只有人事主管可以审批看到所有信息
            if(userRole.equals("人事主管")){
                PageUtils page = scrapService.queryPage(params);
                return page;
            }
        }
        //是普通员工的话
        IPage<ScrapEntity> page = this.page(
                new Query<ScrapEntity>().getPage(params),
                new QueryWrapper<ScrapEntity>()
                        .eq( "user_name",params.get("userName"))
                        .like(StringUtils.isNotBlank((String) params.get("key")),"assets_num",params.get("key"))
        );

        return new PageUtils(page);
    }

    /**
     * 报废
     * @param scrap  // 根据资产编号找
     * 1.把报废信息添加入报废表
     * 2.更新维修表的状态为报废 3
     * 3.更新领用表状态为为报废 7
     * 4.更新资产表状态为报废 4
     */
    @Override
    public void scrapAdd(ScrapEntity scrap) {
        boolean save = scrapService.save(scrap);

        String assetsNum = scrap.getAssetsNum();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assets_num",assetsNum);
        queryWrapper.eq("repairs_status","2");
        //维修表
        RepairsEntity one = repairsService.getOne(queryWrapper);
        one.setRepairsStatus("3");
        repairsService.updateById(one);

        //领用表
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("assets_num",assetsNum);
        queryWrapper1.eq("create_status","2");
        ReceiveEntity one1 = receiveService.getOne(queryWrapper1);
        one1.setCreateStatus("7");

        one1.setScrapTime(scrap.getScrapTime());
        receiveService.updateById(one1);
        //资产表
        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("assets_num",assetsNum);
        InfoEntity one2 = infoService.getOne(queryWrapper2);
        one2.setAssetsStatus("4");
        infoService.updateById(one2);

    }



}